import { Component, OnInit, Input, Output, EventEmitter, ViewChild } from '@angular/core';

import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { PaginationComponent } from '../../../public/pagination/pagination.component';
import { Service } from '../../../server/server';
import { product } from '../product.data';
import {content} from '../../content.data';
import {appContentUrl} from '../../content.url';

declare var $: any;

@Component({
    selector: 'product-content-project',
    templateUrl: './product-content-project.component.html'
})
    export class ProductContentProjectComponent implements OnInit {
    total: number = 0;
    naturalNum: number = 0;
    list: Array<object>;

    isChose: boolean = false;
    queryCriteria: string = '';


    @Input() taskName: string;                              //任务名称
    @Input() projectInfo = [];                              //项目
    projectCurrentInfo = [];                              //当前项目
    @Output() projectEmit = new EventEmitter(); //项目事件

    @ViewChild('pagination') pagination: PaginationComponent;

    taskData: any;
    type: string = '';

    detail = [];

    constructor(
        private server: Service,
        private request: CudTableService,
        private hintsService: HintsService,
        public removeTableService: RemoveTableService) { }


    ngOnInit() {
    }

    getProjectList(val) {
        if(val){
            this.detail = val;
            this.taskName = val.name;
            this.projectInfo = [{
                projectId : val.projectId,
                projectName : val.projectName
            }];
            this._cancelChecked();
            this.getProjectsList();
        }

    }

    setPorjectId(){
        if(this.detail){
            let info = {
                token : product.retrieval.token,
                author : product.retrieval.createBy,
                projectId:this.projectInfo[0]['projectId'],
                productIds:[this.detail['productId']]
            };
            this.request
                .postRequest(this.server.baseUrl + 'Service/Config/updateProductsProject', info)
                .then((res: any) => {
                    this.hintsService.messages(res.message, res.result_code);
                    this.dismissModal();
                    return;
                })
                .catch((err: any) => {
                    this.hintsService.messages('error', 'error');
                    return;
                });
        }
    }

    getProjectsList() {
        this.projectCurrentInfo = this.projectInfo;
        let params = this.getProjectListParams();
        this.request.postRequest(this.server.baseUrl +  'Service/Config/getProjectList', params).then((res: any) => {
            if (res.result_code === '0000') {
                this.list = res.result;
                this.total = res.length;
                this.naturalNum = product.pagination.naturalNum;
                if (this.projectInfo) {
                    for (let item of this.list) {
                        for (let child of this.projectInfo) {
                            if (item['projectId'] == child['projectId']) {
                                item['isChecked'] = true;
                                this.removeTableService.removeChose(true, item, this.list.length)['bool'];
                            }
                        }
                    }
                }
            } else {
                this.hintsService.messages(res.message, res.result_code);
            }
            return;
        }).catch((err: any) => {
            this.hintsService.messages("error", "error");
            return;
        });
    }

    getProjectListParams() {
        return {
            token: product.retrieval.token,
            pageNo: product.pagination.pageNumber,
            pageSize: product.pagination.pageSize,
            startTime: null,
            endTime: null,
            checkStatus: null,
            status: null,
            queryCriteria: this.queryCriteria
        }

    }

    getTaskThemeTableListParams() {
        return {
            token: product.retrieval.token,
            id: product.retrieval.id,
            themeTableId: '',
            pageNo: product.pagination.pageNumber,
            pageSize: product.pagination.pageSize,
            taskId: '',
            queryCriteria: this.queryCriteria
        }
    }

    /**
     * 单选
     * @param bool
     * @param name
     * @param length
     */
    _chose(bool, child, length) {
        //多选
        // if (!bool) {
        //     for (let item of this.list) {
        //         if (item['projectId'] === child.projectId) {
        //             item['isChecked'] = !bool;
        //         }
        //         var index = 0;
        //         for (let info of this.projectCurrentInfo) {
        //             if (info['projectId'] === child.projectId) {
        //                 this.projectCurrentInfo.splice(index,1);
        //             }
        //             index++;
        //         }
        //     }
        // } else {
        //     let isSelected = false;
        //     for (let item of this.list) {
        //         if (item['projectId'] === child.projectId) {
        //             item['isChecked'] = bool;
        //         }
        //         for (let info of this.projectCurrentInfo) {
        //             if (info['projectId'] === child.projectId) {
        //                 isSelected = true;
        //             }
        //         }
        //     }
        //     if(!isSelected){
        //         this.projectCurrentInfo.push({
        //             projectName : child.projectName,
        //             projectId : child.projectId
        //         });
        //     }
        // }

        this.projectInfo = [{
            projectName : child.projectName,
            projectId : child.projectId
        }];

        this.removeTableService.removeChose(bool, child, length)['bool'];
    }

    /**
     * 重置表格复选框、选中的ID
     */
    public _cancelChecked() {
        this.isChose = false;
        this.list = [];
        // this.removeTableService.removeAll(false, '', '');
    }

    public dismissModal() {
        this.detail = [];
        this.server.taskProjectEmit.emit(true);
        $("#distributeProjectTabs").modal('hide');
    }

    clearQueryCriteria() {
        this.projectCurrentInfo = [];
        this.projectInfo = [];
        this.list = [];
        this.queryCriteria = '';
        this.pagination.currentPage = 1;
        this.pagination.pageSize = 20;
        product.pagination.pageNumber = 1;
        product.pagination.pageSize = 20;
        product.pagination.naturalNum = 0;
        this.removeTableService.removeAll(false, '', '');
    }


    /**
     * 搜索
     * @param val
     */
    _retrieval(val: string) {
        this.queryCriteria = val.trim();
        this.getProjectsList();
        // if (!this.type || (this.type === 'edit' && this.taskData.parentId === 0) || this.taskData.parentId === 0) {
        // if (!this.type || (this.type === 'edit' && this.taskData.parentId === 0)) {
        //     this.getProjectsList();
        // } else {
        //     this.getTaskThemeTableList(this.taskData);
        // }
    }
    /**
     * 分页
     * @param pagination
     */
    _pagination(pagination: Array<any>) {
        product.pagination.pageNumber = pagination[0].pageNumber;
        product.pagination.pageSize = pagination[0].pageSize;
        product.pagination.naturalNum = (product.pagination.pageNumber - 1) * product.pagination.pageSize;

        this.getProjectsList();
    }
}

